/*
 * HttpResponse.cpp
 *
 */

#include "HttpResponse.h"
#include "HttpHost.h"
#include "HttpCodes.h"
#include <sstream>

HttpResponse::HttpResponse():body(NULL) {}

HttpResponse::~HttpResponse() {
	if (body != NULL)
		delete body;
}

int HttpResponse::getCode() const {
	return code;
}

void HttpResponse::setCode(int code) {
	this->code = code;
	this->reasonPhrase = HttpCodes::getMessage(code);
}

void HttpResponse::setReasonPhrase(const std::string& reason) {
	this->reasonPhrase = reason;
}

std::string HttpResponse::getStatusLine() const {
	std::stringstream buffer;
	buffer << HttpHost::HTTP_VERSION << SP << code << SP;
	buffer << reasonPhrase << CRLF;
	return buffer.str();
}

bool HttpResponse::hasBody() const {
	return body != NULL;
}

HttpBody& HttpResponse::getBody() const {
	if (body == NULL)
		throw MessageException("No body in message");
	return *body;
}

void HttpResponse::setBody(HttpBody* body) {
	if (this->body != NULL)
		delete this->body;
	this->body = body;
	addHeader(body->getContentLength());
}
